package net.socket.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

	public Server() {
		int port = 10000;
		PrintWriter clientOut = null;
		BufferedReader clientIn = null;
		Socket socket = null;
		ServerSocket server = null;
		try {
			InetAddress ip = InetAddress.getLocalHost();
			server = new ServerSocket(port);
			// System.out.println(server.getLocalPort());
			// System.out.println(server.getLocalSocketAddress());
			// System.out.println(server.getInetAddress());

			while (true) {
				socket = server.accept();
				
				System.out.println("socket=="+socket);
				System.out.println(socket.isBound());
				System.out.println(socket.isConnected());
				clientIn = new BufferedReader(new InputStreamReader(
						socket.getInputStream()));
				clientOut = new PrintWriter(socket.getOutputStream(), true);
				
				String userData = clientIn.readLine();
				String newData = processClientData(userData);

				System.out.println(userData);
				clientOut.println(newData);
				clientOut.close();
				clientIn.close();
				socket.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 处理客户端来的数据
	 * 
	 * @param clientData
	 * @return
	 */
	private String processClientData(String clientData) {
		return clientData + " world";
	}

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) {
		new Server();
	}

}
